//profile 
function userProfileLoad(){
var user_id = JSON.parse(localStorage["user_id"]);
$('#profileLoader').html('<img src="images/ajax-loader.gif"/>');
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'registration/'+user_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				$('#profileLoader').html('');
				var obj = eval("(" + data.userDetailes + ")");
				$.each(obj, function (index,Object) {
						$('#userName').val(Object.user_name);
						$('#userAddress').val(Object.address);
					if(Object.pincode==0){
						var pincode="";
						$('#userPincode').val(pincode);
						$('#button_profile').attr('value','Next');
					}else{
						$('#userPincode').val(Object.pincode);
						$('#button_profile').attr('value','Save');
					}
					if(Object.rating!=null){
						var options="";
						for(var i=2;i<=5;i+=.5){
							if(i==Object.rating){
								options+='<option value="'+i+'" selected=selected>'+i+'</option>';
							}else{
								options+='<option value="'+i+'">'+i+'</option>';
							}
						}
						$('#rating').html(options);
					}
				});	
			}else{
				$('#profileLoader').html('');
			}	
		}
	});
}
//Settings 
function userSettingsLoad(){
var user_id = JSON.parse(localStorage["user_id"]);
$('#settingsLoader').html('<img src="images/ajax-loader.gif"/>');
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'registration/'+user_id,
		dataType	: 	"json",
		success: function(data){
			$('#settingsLoader').html('');
			var obj = eval("(" + data.userDetailes + ")");
			$.each(obj, function (index,Object) {
				if(Object.setting_rating!=null){
					var rating="";
					for(var i=2;i<=5;i+=.5){
						if(i==Object.setting_rating){
							rating+='<option value="'+i+'" selected=selected>'+i+'</option>';
						}else{
							rating+='<option value="'+i+'">'+i+'</option>';
						}
					}
					$('#setting_rating').html(rating);
				}
				if(Object.radius!=null){
					var radius="";
					if(Object.radius!=0){
						for(var i=1;i<=15;i+=1){
							if(i==parseInt(Object.radius)){
								radius+='<option value="'+i+'" selected=selected>'+i+' Miles </option>';
							}else{
								radius+='<option value="'+i+'">'+i+' Miles </option>';
							}
						}
						$('#radius').html(radius);
						$('#button_settings').attr('value','Save');
						go_list_value=2;
					}else{
						for(var i=1;i<=15;i+=1){
							if(i==parseInt(Object.radius)){
								radius+='<option value="'+i+'" selected=selected>'+i+' Miles </option>';
							}else{
								radius+='<option value="'+i+'">'+i+' Miles </option>';
							}
						}
						$('#radius').html(radius);
						$('#button_settings').attr('value','Next');
						go_list_value=1;
					}
				}
				if(Object.available_status==1){
					$("#cmn-toggle-1").prop('checked', false);
				}else{
					$("#cmn-toggle-1").prop('checked', true);
				}
				if(Object.notifications_status==1){
					$("#cmn-toggle-2").prop('checked', false);
				}else{
					$("#cmn-toggle-2").prop('checked', true);
				}
			});	
		}
	});
}	

//view alert
function viewAlertLoad(){
var user_id = JSON.parse(localStorage["user_id"]);
	$('#alertLoader').html('<img src="images/ajax-loader.gif"/>');
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'get_alerts/'+user_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				$('#alertLoader').html('');
				var todayPosts="";
				var view_part_detailes="";
				todayPosts+='<ul>';
				var obj = eval("(" + data.post + ")");
				$.each(obj, function (index,Object) {//alert();return false;
					var userName=Object.user_name;
					userName = userName.toLowerCase().replace(/\b[a-z]/g, function(letter) {
						return letter.toUpperCase();
					});
					var proposed_date_time=convertMysqldate(Object.proposed_date_time);
					var until_date_time=convertMysqldate(Object.until_date_time);
					var venue=Object.venue.split(',');
					todayPosts+='<li><div id="today-post'+Object.user_post_id+'">'+
					'<div class="viewlist_pad">'+
						'<div class="pos_relative">'+
							'<div class="view_list"><p>'+Object.phone+' '+userName+'</p>'+
								'<p>'+Object.post_message+'</p>'+
								'<p>'+venue[0]+'</p>'+
								'<p>'+venue[1]+'</p>'+
								'<p>'+venue[2]+'</p>'+
								'<p>'+proposed_date_time+' To  '+until_date_time+'</p></div>'+
							'<div class="arrow1"></div>'+
							'<div class="arrow"></div>'+
							'<a href="javascript:void(0);"><div class="conform_btn_blue"><p>&#10004;</p></div></a>'+
						'</div>'+
					'</div>'+
				'</div></li>';
					var user_post_id=Object.user_post_id;
					var alertobj = eval("(" + data.alerts + ")");
					$.each(alertobj, function (index,alerts) {
						if(index==user_post_id){
							$.each(alerts, function (index,alertss) {
								if(alertss.status==0){
									var class_color='left_conform_btn_red';
								}else{
									var class_color='left_conform_btn';
								}
								var partnerName=alertss.partner_name;
								partnerName = partnerName.toLowerCase().replace(/\b[a-z]/g, function(letter) {
									return letter.toUpperCase();
								});
								var pvenue=Object.venue.split(',');
								todayPosts+='<input type="hidden" id="partnerName'+alertss.user_post_replies_id+'" name="partnerName'+alertss.user_post_replies_id+'" value="'+partnerName+'">';
								todayPosts+='<li><div class="viewlist_mar_t" id="todatAlerts'+alertss.user_post_replies_id+'">'+
									'<div class="viewlist_pad pos_relative">'+
									'<div class="pos_relative">'+
										'<div class="view_list_right"><p><a href="javascript:void(0);" onclick="sendCall('+alertss.user_post_replies_id+')">'+alertss.partner_phone+' <a href="javascript:void(0);" onclick="ParDetailesFun('+alertss.user_post_replies_id+')">'+partnerName+'</a></p>'+
											'<p>'+Object.post_message+'</p>'+
											'<p>'+pvenue[0]+'</p>'+
											'<p>'+pvenue[1]+'</p>'+
											'<p>'+pvenue[2]+'</p>'+
											'<p>'+proposed_date_time+' To  '+until_date_time+'</p></div>'+
										'<div class="arrow_left_move"><div class="arrow1"></div>'+
										'<div class="arrow"></div></div><input type="hidden" id="send_call_id'+alertss.user_post_replies_id+'" value="'+alertss.partner_phone+'">';
										if(alertss.status==0){
											todayPosts+='<a id="open-pop-up-2'+alertss.user_post_replies_id+'" href="#pop-up-1" onclick="openPopup('+alertss.user_post_replies_id+','+Object.user_post_id+','+alertss.partner_id+')"><div class="'+class_color+'"><p>&#10004;</p></div></a>'+
											'<p style="display:none" id="deviceID'+alertss.user_post_id+'">'+alertss.partner_device_id+'</p>'+'<input type="hidden" id="pat_platform'+alertss.user_post_id+'" value="'+alertss.partner_platform+'"/><p style="display:none" id="p_time'+alertss.user_post_id+'">'+proposed_date_time+'</p><p style="display:none" id="u_time'+alertss.user_post_id+'">'+until_date_time+'</p>';
										}else{
											todayPosts+='<a href="javascript:void(0);"><div class="'+class_color+'"><p>&#10004;</p></div></a>';
										}
										
									todayPosts+='</div>'+
								'</div>'+
							'</div></li>';
							view_part_detailes+='<div id="partner_det'+alertss.user_post_replies_id+'" class="popup_text">'+
							'<p><h4>Partner Detailes</h4></p>'+ 
							'<div class="input_field input_popup_t">'+
								'<table><tr><td>PartnerName</td><td>:</td><td>'+partnerName+'</td></tr>' +
								'<tr><td>PhoneNumber</td><td>:</td><td>'+alertss.partner_phone+'</td></tr>' +
								'<tr><td>Address</td><td>:</td><td>'+alertss.partner_address+'</td></tr>' +
								'<tr><td>Pincode</td><td>:</td><td>'+alertss.partner_pincode+'</td></tr>' +
								'<tr><td>Proposed date</td><td>:</td><td>'+proposed_date_time+'</td></tr>' +
								'<tr><td>Until date</td><td>:</td><td>'+until_date_time+'</td></tr></table>' +
							'</div>'+
							'</div>';
							});
						}
					});			
				});
				todayPosts+='</ul>';
				$('#today_posts').html(todayPosts);				
				$('#p_det_pop').html(view_part_detailes);
			}else{
				$('#alertLoader').html('');
				$('#todayalerts').html("NO ALERTS FOUND!");	
			}
		}
	});
}
//get Path
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}  
//confirm alerts
function confirmAlertsLoad(){
var user_post_replies_id=getParameterByName('id');
	var user_id = JSON.parse(localStorage["user_id"]);
	$.ajax({
		type		:	'POST',
		url			:  	webServiceUrl+'get_alerts',
		dataType	: 	"json",
		data		:	{ user_post_replies_id:user_post_replies_id,user_id:user_id },
		success: function(data){
			if(data.value==1){
				var viewpartnerDetailes="";
				var todayPosts="";
				var uname="";
				//todayPosts+='<div class="viewlist_pad today_title"><h3>Toady</h3></div>';
				var obj = eval("(" + data.data + ")");
				$.each(obj, function (index,Object) {
					var userName=Object.user_name;
					userName = userName.toLowerCase().replace(/\b[a-z]/g, function(letter) {
						return letter.toUpperCase();
					});
					var venue=Object.venue.split(',');
					var proposed_date_time=convertMysqldate(Object.proposed_date_time);
					var until_date_time=convertMysqldate(Object.until_date_time);
					todayPosts+='<div id="today-post'+Object.user_post_id+'">'+
					'<div class="viewlist_pad">'+
						'<div class="pos_relative">'+
							'<div class="view_list"><p>'+Object.phone+' '+userName+'</p>'+
								'<p>'+Object.post_message+'</p>'+
								'<p>'+venue[0]+'</p>'+
								'<p>'+venue[1]+'</p>'+
								'<p>'+venue[2]+'</p>'+
								'<p>'+proposed_date_time+' To '+until_date_time+'</p></div>'+
							'<div class="arrow1"></div>'+
							'<div class="arrow"></div>'+
							'<a href="javascript:void(0);" ><div class="conform_btn_blue"><p>&#10004;</p></div></a>'+
						'</div>'+
					'</div>'+
					'</div>';
					var partnerName=Object.partner_name;
					partnerName = partnerName.toLowerCase().replace(/\b[a-z]/g, function(letter) {
						return letter.toUpperCase();
					});
					var par_venue=Object.venue.split(',');
					todayPosts+='<div class="viewlist_mar_t" id="todatAlerts'+Object.user_post_replies_id+'">'+
					'<div class="viewlist_pad">'+
						'<div class="pos_relative">'+
							'<div class="view_list"><p>'+Object.partner_phone+' <a href="javascript:void(0)" onclick="OpenPartnerDetailesPopUp()">'+partnerName+'</a></p>'+
								'<p>'+Object.partner_pincode+'</p>'+
								'<p>'+par_venue[0]+'</p>'+
								'<p>'+par_venue[1]+'</p>'+
								'<p>'+par_venue[2]+'</p>'+
								'<p>'+proposed_date_time+' To '+until_date_time+'</p></div>'+
							'<div class="arrow1"></div>'+
							'<div class="arrow"></div>'+
							'<a href="javascript:void(0);" ><div class="conform_btn"><p>&#10004;</p></div></a>'+
						'</div>'+
					'</div>'+
				'</div>';
				viewpartnerDetailes+='<div class="popup_text">'+
						'<p><h4>Partner Detailes</h4></p>'+ 
						'<div class="input_field input_popup_t">'+
							'<table><tr><td>PartnerName</td><td>:</td><td>'+partnerName+'</td></tr>' +
							'<tr><td>PhoneNumber</td><td>:</td><td>'+Object.partner_phone+'</td></tr>' +
							'<tr><td>Address</td><td>:</td><td>'+Object.partner_address+'</td></tr>' +
							'<tr><td>Pincode</td><td>:</td><td>'+Object.partner_pincode+'</td></tr>' +
							'<tr><td>Proposed date</td><td>:</td><td>'+proposed_date_time+'</td></tr>' +
							'<tr><td>Until date</td><td>:</td><td>'+until_date_time+'</td></tr></table>' +
						'</div>'+
						'</div>';
				});
				$('#today_posts').html(todayPosts);	
				$('#partner_detailes').html(viewpartnerDetailes);
			}
		}
	});
}
//get lists
function getListsLoad(){
var user_id = JSON.parse(localStorage["user_id"]);
$('#listLoader').html('<img src="images/ajax-loader.gif"/>');
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'get_lists/'+user_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				$('#listLoader').html('');
				var todaylists="";
				var view_partner_detailes="";
				var confirmation_pop_up="";
				todaylists+='<ul id="thelist">';
				var Object = eval("(" + data.lists + ")");
					$.each(Object, function (index,getLists){
						var userName=getLists.user_name;
						userName = userName.toLowerCase().replace(/\b[a-z]/g, function(letter) {
							return letter.toUpperCase();
						});
						var proposed_date_time=convertMysqldate(getLists.proposed_date_time);
						var until_date_time=convertMysqldate(getLists.until_date_time);
						var venue_detailes=getLists.venue.split(',');
						todaylists+='<input type="hidden" id="partnerName'+getLists.user_post_id+'" name="partnerName'+getLists.user_post_id+'" value="'+userName+'">';
						todaylists+='<li><div id="today-post'+getLists.user_post_id+'">'+
						'<div class="viewlist_pad">'+
							'<div class="pos_relative">'+
								'<div class="view_list"><p><a href="javascript:void(0);" onclick="sendCall('+getLists.user_post_id+')">'+getLists.phone+' <a href="javascript:void(0);" onclick="OpenPartnerDetailes('+getLists.user_post_id+')">'+userName+'</a></p>'+
									'<p>'+getLists.post_message+'</p>'+
									'<p>'+venue_detailes[0]+'</p>'+
									'<p>'+venue_detailes[1]+'</p>'+
									'<p>'+venue_detailes[2]+'</p>'+
									'<p>'+proposed_date_time+' To  '+until_date_time+'</p></div>'+
								'<div class="arrow1"></div>'+
								'<div class="arrow"></div><input type="hidden" id="send_call_id'+getLists.user_post_id+'" value="'+getLists.phone+'">';
								if(getLists.upastatus==0){
									todaylists+='<a id="open-pop-up-2'+getLists.user_post_id+'" href="javascript:void(0)"><div class="conform_btn_red"><p>&#10004;</p></div></a>';
								}else if(getLists.upastatus==1){
									todaylists+='<a id="open-pop-up-2'+getLists.user_post_id+'" href="javascript:void(0)"><div class="conform_btn"><p>&#10004;</p></div></a><p style="display:none" id="p_time'+getLists.user_post_id+'">'+proposed_date_time+'</p><p style="display:none" id="u_time'+getLists.user_post_id+'">'+until_date_time+'</p>';
								}else{
									todaylists+='<div id="red'+getLists.user_post_id+'"><a id="open-pop-up-2'+getLists.user_post_id+'" href="#pop-up-1" onclick="openPopupV('+getLists.user_post_id+','+getLists.user_id+')"><div class="conform_btn_blue"><p>&#10004;</p></div></a></div>'+
									'<p style="display:none" id="deviceID'+getLists.user_post_id+'">'+getLists.device_id+'</p>'+'<input type="hidden" id="p_platform'+getLists.user_post_id+'" value="'+getLists.platform+'"/><p style="display:none" id="p_time'+getLists.user_post_id+'">'+proposed_date_time+'</p><p style="display:none" id="u_time'+getLists.user_post_id+'">'+until_date_time+'</p>';
									
								}
								todaylists+='</div>'+
							'</div>'+
						'</div></li>';
						if(getLists.confirm_status==1){
							var postIds=$("#hid_post_id").val();
							$("#hid_post_id").val(postIds+','+getLists.user_post_id);
						}
						view_partner_detailes+='<div id="patid'+getLists.user_post_id+'" class="popup_text">'+
						'<p><h4>Partner Detailes</h4></p>'+ 
						'<div class="input_field input_popup_t">'+
							'<table><tr><td>PartnerName</td><td>:</td><td>'+userName+'</td></tr>' +
							'<tr><td>PhoneNumber</td><td>:</td><td>'+getLists.phone+'</td></tr>' +
							'<tr><td>Address</td><td>:</td><td>'+getLists.address+'</td></tr>' +
							'<tr><td>Pincode</td><td>:</td><td>'+getLists.pincode+'</td></tr>' +
							'<tr><td>Proposed date</td><td>:</td><td>'+proposed_date_time+'</td></tr>' +
							'<tr><td>Until date</td><td>:</td><td>'+until_date_time+'</td></tr></table>' +
						'</div>'+
						'</div>';
						
						confirmation_pop_up+='<div id="confirm'+getLists.user_post_id+'" class="popup_text">'+
							'<p><h4>Your request is confirmed.</h4></p>'+ 
							'<div class="input_field input_popup_t">'+
								'<table><tr><td>PartnerName</td><td>:</td><td>'+userName+'</td></tr>' +
								'<tr><td>Contact no</td><td>:</td><td>'+getLists.phone+'</td></tr>' +
								'<tr><td>Proposed date</td><td>:</td><td>'+proposed_date_time+'</td></tr>' +
								'<tr><td>Until date</td><td>:</td><td>'+until_date_time+'</td></tr></table>' +
							'</div>'+
							'<div class="popup_btns" >'+
							'<div class="popup_row">'+
								'<a href="javascript:void(0);"><div class="ok1" onclick="changeStatusConfirm('+getLists.user_post_replies_id+','+getLists.user_post_id+')">Ok</div></a>'+
							'</div>'+
								'<div class="clearfix"></div>'+
							'</div>'+
						'</div>';
						
					});
					todaylists+='</ul>';
					$('#today_posts').html(todaylists);				
					$('#p_detailes').html(view_partner_detailes);				
					$('#c_detailes').html(confirmation_pop_up);
					var postIds=$("#hid_post_id").val();
					if(postIds!=0){
						var allIds=postIds.split(',');
						for(var i=1;i<=(allIds.length)-1;i++){
							$('#confirm'+allIds[i]).popUpWindow({action: "open"});
							$('.close').remove();
							if(allIds.length==2){
								var newIds=postIds.replace(','+allIds[i], ""); 
							}else{
								var newIds=postIds.replace(allIds[i]+',', ""); 
							}
							$("#hid_post_id").val(newIds);
							$("#hid_post_id_remove").val(allIds[i]);
							break;
						}
					}	
			}else{
				$('#listLoader').html('');
				$("#today_lists").html("NO LISTS FOUND!");
			}
		}
	});
}
function changeStatusConfirm(user_post_replies_id,user_post_id){
var p_time=$('#p_time'+user_post_id).html();
var u_time=$('#u_time'+user_post_id).html();
	$.ajax({
		type		:	'PUT',
		url			:  	webServiceUrl+'add_post/'+user_post_replies_id,
		dataType	: 	"json",
		data		: 	{status:0},
		success: function(data){
			if(data.value==1){
			var removeId=$("#hid_post_id_remove").val();
				$('#confirm'+removeId).popUpWindow({action: "close"});
				var postIds=$("#hid_post_id").val();
				if(postIds!=0){
					var allIds=postIds.split(',');
					for(var i=1;i<=(allIds.length)-1;i++){
						$('#confirm'+allIds[i]).popUpWindow({action: "open"});
						$('.close').remove();
						if(allIds.length==2){
							var newIds=postIds.replace(','+allIds[i], ""); 
						}else{
							var newIds=postIds.replace(allIds[i]+',', ""); 
						}
						$("#hid_post_id").val(newIds);
						$("#hid_post_id_remove").val(allIds[i]);
						break;
					}
				}
			}
		}
	});
}
//list and alert request partner details

function OpenPartnerDetailes(userpostId){
	$('#patid'+userpostId).popUpWindow({action: "open"});
}

function ParDetailesFun(pat_repl_id){
	$('#partner_det'+pat_repl_id).popUpWindow({action: "open"});
}

function OpenPartnerDetailesPopUp(){
	$('#partner_detailes').popUpWindow({action: "open"});
}
function sendCall(id){
	var phone_number=$('#send_call_id'+id).val();
	alert(phone_number);
}